<template>
  <div style="margin: 10px"></div>
  <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="地点" prop="address" />
    <el-table-column label="服务热线" prop="phone" />
    <el-table-column align="right">
      <template #header>
        <el-input v-model="search" size="small" placeholder="请输入名称" />
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import { computed, reactive, ref } from "vue";
const search = ref("");
const ruleFormRef = ref(null);
const activeList = ref([
  {
    id: 1,
    address: "北京市",
    phone: "12345",
  },
  {
    id: 2,
    address: "上海市",
    phone: "12319",
  },
  {
    id: 3,
    address: "广东省广州市",
    phone: "12345",
  },
  {
    id: 4,
    address: "四川省成都市",
    phone: "12345",
  },
  {
    id: 5,
    address: "陕西省西安市",
    phone: "12345",
  },
  {
    id: 6,
    address: "湖南省长沙市",
    phone: "12345",
  },
  {
    id: 7,
    address: "贵州省贵阳市",
    phone: "12345",
  },
]);
const dialogVisible = ref(false);
const title = ref("编辑信息");
const form = reactive({
  id: "",
  name: "",
  content: "",
});
const rules = reactive({
  name: [
    {
      required: true,
      message: "请输入活动名称",
      trigger: "blur",
    },
  ],
  content: [
    {
      required: true,
      message: "请输入活动内容",
      trigger: "blur",
    },
  ],
});
const filterTableData = computed(() =>
  activeList.value.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
);
const handleEdit = (index, row) => {
  form.id = row.id;
  form.name = row.name;
  form.content = row.content;
  title.value = "编辑信息";
  dialogVisible.value = true;
};

const handleClose = (done) => {
  done();
};

const close = () => {
  form.id = "";
  form.name = "";
  form.content = "";
  dialogVisible.value = false;
};
const handleAdd = () => {
  form.id = "";
  form.name = "";
  form.content = "";
  title.value = "新增活动";
  dialogVisible.value = true;
};

const handleDelete = (index, row) => {
  activeList.value = activeList.value.filter((v) => v.id !== row.id);
  localStorage.setItem("activeList", JSON.stringify(activeList.value));
};

const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (title.value === "新增活动") {
        form.id = activeList.value.length + 1;
        activeList.value.push({
          id: form.id,
          name: form.name,
          content: form.content,
        });
      } else {
        activeList.value = activeList.value.map((item) => {
          if (item.id === form.id) {
            return {
              id: form.id,
              name: form.name,
              content: form.content,
            };
          } else {
            return item;
          }
        });
      }
      dialogVisible.value = false;
      form.id = "";
      form.name = "";
      form.content = "";
      localStorage.setItem("activeList", JSON.stringify(activeList.value));
    } else {
      console.log("error submit!", fields);
    }
  });
};

const init = () => {
  let local = JSON.parse(localStorage.getItem("activeList"));
  if (local) {
    activeList.value = local;
  }
};
init();
</script>

<style lang="less" scoped>
</style>